package com.fw.payment.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.codehaus.jettison.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping(value = "/simulate")
public class SimulateController {

    private static final Logger log = Logger.getLogger(SimulateController.class);

    @RequestMapping(value = "/response_back", method = { RequestMethod.GET, RequestMethod.POST })
    public String responseBack(ModelMap model) {
	log.debug("response_back");
	return "simulate/response_back";
    }

    @RequestMapping(value = "/ec", method = RequestMethod.GET)
    public String ec(ModelMap model) {
	log.debug("get simulate");
	return "simulate/ec";
    }

    @RequestMapping(value = "/ec_success", method = RequestMethod.GET)
    public String ecSuccess(ModelMap model) {
	log.debug("ec_success");
	return "simulate/ec_success";
    }

    @RequestMapping(value = "/ec_cancel", method = RequestMethod.GET)
    public String ecCancel(ModelMap model) {
	log.debug("ec_cancel");
	return "simulate/ec_cancel";
    }

    @RequestMapping(value = "/ec_fail", method = RequestMethod.GET)
    public String ecFail(ModelMap model) {
	log.debug("ec_fail");
	return "simulate/ec_fail";
    }

    @RequestMapping(value = "/payatall_paynow", method = { RequestMethod.GET, RequestMethod.POST })
    public String payatallPaynow(ModelMap model) {
	log.debug("post payatall_paynow");
	return "simulate/payatall_paynow";
    }

    @RequestMapping(value = "/payatall_confirm", method = { RequestMethod.GET, RequestMethod.POST })
    public String payatallConfirm(ModelMap model) {
	log.debug("post payatall_confirm");
	return "simulate/payatall_confirm";
    }

    @RequestMapping(value = "/payatall_inquiry", method = { RequestMethod.GET, RequestMethod.POST })
    public void payatallInquiry(ModelMap model, HttpServletRequest request, HttpServletResponse response) throws IOException {
	log.debug("post payatall_inquiry");
	PrintWriter out = response.getWriter();
	response.setContentType("text/html; charset=UTF-8");

	JSONObject obj = new JSONObject();
	try {
	    obj.put("mid", new Integer(2014020701));
	    obj.put("ptransid", new Integer(1101201401));
	    obj.put("inv", request.getParameter("inv"));
	    obj.put("serviceid", new String("02"));
	    obj.put("status", new String("S"));
	    obj.put("amt", request.getParameter("amt"));
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
	    obj.put("payment_date", sdf.format(new Date()));
	    obj.put("url", new String("http://url.co.th/barcode" + new Date().getTime()));
	    obj.put("paycode", new Integer(1101201401));
	} catch (Exception e) {
	    e.printStackTrace();
	}

	out.print(obj);
	out.flush();
	out.close();
    }
}
